/*
 *	작성날짜 : 10월 16일
 *	작성자 : 이민선 	
 */
package jangdolbaeng2.customer.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import jangdolbaeng2.db.MybatisConfig;
import jangdolbaeng2.vo.CartVO;
import jangdolbaeng2.vo.DeliveryVO;
import jangdolbaeng2.vo.Order_detailVO;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.Product_orderVO;
import jangdolbaeng2.vo.RealCartVO;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class CartDAO {
	private SqlSessionFactory factory = MybatisConfig.getSqlSessionFactory();
	
	//장바구니 목록 뿌려주기
	public List<RealCartVO> selectCart(String customer_id) throws SQLException{
		SqlSession ss = null;
		//List<HashMap<String, Object>> cartList = null;
		List<RealCartVO> realCart = null;
		//System.out.println("들어왔따");
		try {
			ss = factory.openSession();
			realCart = ss.selectList("cart.selectCart", customer_id);
			//System.out.println("cartList DAO : " + realCart);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		
		return realCart;
	}
	
	//배송지정보 저장
	   public void insertDeli(DeliveryVO delivery) throws SQLException{
	      SqlSession ss = null;
	      try {
	         ss = factory.openSession();
	         ss.insert("cart.insertDeli", delivery);
	         //System.out.println("DAO Delivery : "+delivery);
	         ss.commit();
	      } catch (Exception e) {
	         // TODO: handle exception
	         e.printStackTrace();
	      } finally {
	         ss.close();
	      }
	   }
	   
	   //product_order 저장
	   public void insertPro_Order(Product_orderVO product_order) throws SQLException{
	      SqlSession ss = null;
	      try {
	         ss = factory.openSession();
	         ss.insert("cart.insertPro_Order", product_order);
	         //System.out.println("DAO product_order : "+ product_order);
	         ss.commit();
	      } catch (Exception e) {
	         // TODO: handle exception
	            e.printStackTrace();
	      } finally {
	         ss.close();
	      }
	   }
	
	   //product_order에서 order_seq값만 가지고 오기
	   public int selectPro_Order() throws SQLException {
		   SqlSession ss = null;
		   int result = 0;
		   
		   try {
			   ss = factory.openSession();
			   result = ss.selectOne("cart.selectPro_Order");
			   ss.commit();
		   } catch (Exception e) {
			   // TODO: handle exception
			   e.printStackTrace();
		   }finally {
			   ss.close();
		   }
		   
		   return result;
	   }
	   
	   //배송지 정보 select
	   public DeliveryVO selectDeli(int order_seq) throws SQLException{
		   SqlSession ss = null;
		   DeliveryVO delivery = null;
		   try {
			   ss = factory.openSession();
			   delivery = ss.selectOne("cart.selectDeli", order_seq);
			   //System.out.println("DAO order_seq : " + order_seq);
			   ss.commit();
		   } catch (Exception e) {
			   // TODO: handle exception
			   e.printStackTrace();
		   }finally {
			   ss.close();
		   }
		   return delivery;
	   }
	   
	   //order_detail 저장
	   public void insertOrderDetail (List<RealCartVO> realCart) throws SQLException{
		   int result = 0;
		   
		   SqlSession ss = null;
		   
		   try {
			   	ss = factory.openSession();
			   	
			   	for(RealCartVO vo: realCart) {
			   		result = ss.insert("cart.insertOrderDetail", vo);
			   		System.out.println("DAO> insertOrderDetail(): " + result);
			   		ss.commit();
			   	}			   	
		   } catch (Exception e) {
			   // TODO: handle exception
			   e.printStackTrace();
		   } finally {
			   ss.close();
		   }		   
		 
	   }
	   
	   //장바구니에서 선택 상품 삭제
	   public void deleteCart(CartVO cart){
		   SqlSession ss = null;
		   try {
				ss = factory.openSession();
				ss.delete("cart.deleteCart", cart);
				//System.out.println("DAO cart : " + cart);
				ss.commit();
		   } catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				ss.close();
			}
	   }
	   
	   //장바구니 주문 후 전체 삭제
	   public void allDeleteCart(String customer_id) {
		   SqlSession ss = null;
		   try {
			   ss = factory.openSession();
			   ss.delete("cart.allDeleteCart", customer_id);
			   System.out.println("DAO> allDeleteCart: customer_id : " + customer_id);
			   ss.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			ss.close();
		}
	   }
	/*---------------------------------- 메소드 작성자 : 문연지-----------------------------------*/
	public void insertCart(CartVO cart) {
		SqlSession ss = null;
		
		try {
			ss = factory.openSession();
			ss.insert("cart.insertCart", cart);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		
	}
	public CartVO selectACart(CartVO newcart) {
		SqlSession ss = null;
		CartVO cart = null;
		
		try {
		
			ss = factory.openSession();
			cart = ss.selectOne("cart.selectACart", newcart);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		
		return cart;
	}
	
	public void updateCart(CartVO newcart) {
		SqlSession ss = null;
		
		try {
			ss = factory.openSession();
			ss.update("cart.updateCart", newcart);
			//System.out.println("DAO newcart : " + newcart);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
	}

	public int getSeq() {
		int order_seq = 0;
		
		SqlSession ss = null;		
		try {
			ss = factory.openSession();
			order_seq = ss.selectOne("cart.getSeq");
			//System.out.println("DAO getSeq() : " + order_seq);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		
		return order_seq;
	}

	public ProductVO getProduct(Integer product_seq) {
		
		SqlSession ss = null;
		ProductVO product = null;
		
		try {
		
			ss = factory.openSession();
			product = ss.selectOne("cart.selectProduct", product_seq);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
		
		return product;
	}
	
}
